package com.deu.tsf.controller;

import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.plugins.Page;
import com.deu.tsf.common.result.JsonResult;
import com.deu.tsf.entity.VehicleInfo;
import com.deu.tsf.service.IVehicleInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @Description: 车辆管理控制层
* @author Y.Jin
* @date 2016年12月15日 上午9:33:00
 */
@Controller
@RequestMapping("/vehicle")
@Api(tags="车辆管理",produces="abc")
public class VehicleManagerController extends BaseController{
	
	@Autowired
	private IVehicleInfoService viService;
	
	private static final String VI_LIST = "vilist";
	private static final String VI_EDIT = "viedit";
	
	/**
	 * 空字段检测
	 * @param vInfo
	 * @return
	 */
	private JsonResult null_check(VehicleInfo vInfo){
		boolean flag = vInfo.getConstructionTeam3() == null || "".equalsIgnoreCase(vInfo.getConstructionTeam3()) 
				|| vInfo.getBds_id() == null || "".equalsIgnoreCase(vInfo.getBds_id()) 
				|| vInfo.getDriver() == null || "".equalsIgnoreCase(vInfo.getDriver())
				|| vInfo.getMaintain() == null || "".equalsIgnoreCase(vInfo.getMaintain())
				|| vInfo.getManager() == null || "".equalsIgnoreCase(vInfo.getManager())
				|| vInfo.getVehicleNO() == null || "".equalsIgnoreCase(vInfo.getVehicleNO())
				|| vInfo.getVehicleType() == null;
		JsonResult json = new JsonResult();
		if(flag)
		{
			json.setSuccess(false);
			json.setCode(-1);
			json.setMsg("参数中不能包含空值");
		}
		return json;
	}
	
	/**
	 * 获取车辆信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value="获取车辆信息")
	@RequestMapping(value = "{id}/get", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get(@PathVariable Integer id){
		JsonResult json = new JsonResult();
		VehicleInfo vInfo = viService.selectById(id);
		json.setData(vInfo);
		return json;
	}
	
	/**
	 * 进入编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String edit(Integer id,Model model){
		VehicleInfo vInfo = viService.selectById(id);
		model.addAttribute("vInfo", vInfo);
		return VI_EDIT;
	}
	
	/**
	 * 添加车辆信息 Json格式的方式
	 * @param vInfo
	 * @return
	 */
	@ApiOperation(value="添加车辆信息",code=10086)
	@RequestMapping(value="addForJson",method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(VehicleInfo vInfo){
		JsonResult json = null_check(vInfo);
		
		if(!json.isSuccess()){
			return json;
		}
		
		viService.insert(vInfo);
		json.setData(vInfo);
		return json;	
	}
	
	/**
	 * 更新车辆信息 Json格式
	 * @param vInfo
	 * @return
	 */
	@RequestMapping(value = "update",method=RequestMethod.POST)
	public String _update(VehicleInfo vInfo){
		viService.updateById(vInfo);
		return VI_LIST;	
	}
	
	/**
	 * 采用Json格式更新返回
	 * @param vInfo
	 * @return
	 */
	@ApiOperation(value = "更新车辆信息 ", response=VehicleInfo.class,notes="用于更新车辆的信息",nickname="NICKNAME")
	@RequestMapping(value="updateJson",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult update(VehicleInfo vInfo){
		JsonResult json = new JsonResult();
		viService.updateById(vInfo);
		return json;
	}
	
	/**
	 * 删除车辆信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value="删除车辆信息")
	@RequestMapping(value="{id}/del",method = RequestMethod.GET)
	public String del(HttpServletRequest request,@PathVariable Integer id,Model model){
		if(id == null){
			throw new NullPointerException("ID");
		}
		viService.deleteById(id);
		
		Page<VehicleInfo> page = getPage(request);
		Page<VehicleInfo> pageList = viService.selectPage(page, null);
		model.addAttribute("pageInfo", pageList);
		return VI_LIST;
	}
	
	/**
	 * 车辆信息录入
	 * @param vi
	 * @return
	 */
	@RequestMapping(value = "add",method = RequestMethod.POST)
	public String add(VehicleInfo vi,HttpServletRequest request,Model model){
		System.out.println("新增数据");
		vi.setId(new Random().nextInt(100000));
		boolean success = viService.insert(vi);
		if(success){
			System.out.println("新增数据成功");
		}
		
		Page<VehicleInfo> page = getPage(request);
		Page<VehicleInfo> pageList = viService.selectPage(page, null);
		model.addAttribute("pageInfo", pageList);
		return VI_LIST;
	}
	
	/**
	 * <p>
	 * 车辆信息列表 JSON 格式
	 * </p>
	 * @return
	 */
	@ApiOperation(value = "车辆信息列表",response=Page.class)
	@RequestMapping(value="listJson",method = RequestMethod.GET)
	@ResponseBody
	public JsonResult list(Page<VehicleInfo> page){
		JsonResult json = new JsonResult();
		json.setData(viService.selectPage(page, null));
		return json;
	}
	
	/**
	 * 车辆信息列表
	 * @return
	 */
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list(HttpServletRequest request,Model model){
		Page<VehicleInfo> page = getPage(request);
		Page<VehicleInfo> pageList = viService.selectPage(page, null);
		model.addAttribute("pageInfo", pageList);
		return VI_LIST;
	}
}
